﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using ASBCC.Database.EntityFramework;

namespace BEGDC.CC.Web.Admin
{
    /// <summary>
    /// TODO
    /// </summary>
    public partial class PageView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //TODO: remove logic code to BL
        protected void UpdateToDB_Click(object sender, EventArgs e)
        {
            string updatePages1 = Request.Form["pagecheckbox"];
            string[] updatePages;
            List<PageInfo> pageInfos = new List<PageInfo>();

            if (!String.IsNullOrEmpty(updatePages1))
            {
                updatePages = updatePages1.Split(',');
                foreach (var str_updatePage in updatePages)
                {
                    string[] pageAndBelongTo = str_updatePage.Split('|');
                    PageInfo pageInfo = new PageInfo();
                    pageInfo.FullName = pageAndBelongTo[0];
                    pageInfo.BelongsTo = pageAndBelongTo[1];
                    pageInfos.Add(pageInfo);
                }
            }

            using (var dc = new CCEntities())
            {
                int level = (int)AccessLevel.Disabled;

                foreach (var info in pageInfos)
                {
                    var result = dc.AspxPages.Where(p => p.FullName == info.FullName && p.BelongsTo == info.BelongsTo
                        && p.AccessLevel > level).ToArray();
                    if (result == null || result.Count() == 0)
                    {
                        dc.AddToAspxPages(new AspxPage { FullName = info.FullName, BelongsTo = info.BelongsTo, AccessLevel = (int)AccessLevel.Read });
                        dc.AddToAspxPages(new AspxPage { FullName = info.FullName, BelongsTo = info.BelongsTo, AccessLevel = (int)AccessLevel.Write });
                    }
                    else
                    {
                        foreach (var info1 in result)
                        {
                            info1.Disabled = false;
                        }
                    }

                }
                dc.SaveChanges();

                var dbPages = dc.AspxPages.Where(p => p.AccessLevel > level && !p.Disabled).ToArray();
                if (dbPages != null && dbPages.Count() != 0)
                {
                    foreach (var dbPage in dbPages)
                    {
                        bool flag = true;
                        foreach (var info in pageInfos)
                        {
                            if (info.BelongsTo == dbPage.BelongsTo && info.FullName == dbPage.FullName)
                            {
                                flag = false;       
                                break;
                            }
                        }
                        if (flag)
                        {
                            dbPage.Disabled = true;
                            dc.SaveChanges();
                        }
                        //PageInfo pageInfo = new PageInfo();
                        //pageInfo.FullName = dbPage.FullName;
                        //pageInfo.BelongsTo = dbPage.BelongsTo;
                        //if (!pageInfos.Contains(pageInfo))
                        //{
                        //    dbPage.AccessLevel = level;
                        //    dbPage.Disabled = false;
                        //    dc.SaveChanges();
                        //}

                    }
                }

            }

            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.Admin, "Page Access Management has been operator. " + "Synchronize some pages to the database");


            Response.Redirect(Request.RawUrl);
        }


    }
}
